package com.work.straighttalk.model;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.work.straighttalk.R;
import com.work.straighttalk.app.StraightTalk;
import com.work.straighttalk.bean.Contact;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/18.
 */
public class ContactProvider {
    private ContentResolver cr;
    private String[] strings = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.PHOTO_ID, "sort_key"};

    public ContactProvider() {
        this.cr = StraightTalk.applicationContext.getContentResolver();
    }

    public void getContacts(Uri uri, ContactCallBack contactCallBack) {
        List<Contact> contactList;
        Cursor cs;
        if (null != uri) {
            cs = cr.query(uri, strings, null, null, null);
        } else {
            cs = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, strings, null, null, "sort_key");
        }
        if (cs.moveToFirst()) {
            contactList = new ArrayList<>();
            String sortKey = null, phoneNumber, phoneName;
            Bitmap bitmap;
            do {
                //用户电话号码
                phoneNumber = cs.getString(1);
                if (TextUtils.isEmpty(phoneNumber))
                    continue;
                //用户名
                phoneName = cs.getString(0);
                sortKey = phoneName.substring(0, 1);
                Long phoneID = cs.getLong(2);
                Long photoID = cs.getLong(3);
                bitmap = null;
                //获取用户头像的方法
                if (photoID > 0) {
                    Uri u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, phoneID);
                    InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(cr, u);
                    bitmap = BitmapFactory.decodeStream(inputStream);
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    //Todo:需要一个头像占位图
                    bitmap = BitmapFactory.decodeResource(StraightTalk.getContext().getResources(), R.mipmap.ic_launcher);
                }
                Contact contact = new Contact();
                contact.setName(phoneName);
                contact.setPhoneNumber(phoneNumber);
                contactList.add(contact);
                //插入数据
            } while (cs.moveToNext());
            //获取成功后
            contactCallBack.onSuccess(contactList);
//            listener.onLoadSuccess(contactList);
        } else {
            contactCallBack.onError();
//            listener.onLoadError();
        }
        cs.close();
    }

    interface ContactCallBack {
        void onSuccess(List<Contact> contacts);

        void onError();
    }
}
